package com.woniuxy.ams.service.impl;

import com.qiniu.util.StringUtils;
import com.woniuxy.ams.dao.*;
import com.woniuxy.ams.entity.*;
import com.woniuxy.ams.service.DailyAssessDetailsService;
import com.woniuxy.ams.utils.String2Int;
import com.woniuxy.ams.utils.SystemUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class DailyAssessDetailsServiceImpl implements DailyAssessDetailsService {
    /**
     * 自动注入 dailyAssessDetailsDao
     */
    DailyAssessDetailsDao dailyAssessDetailsDao;
    @Autowired(required = false)
    public void setDailyAssessDetailsDao(DailyAssessDetailsDao dailyAssessDetailsDao) {
        this.dailyAssessDetailsDao = dailyAssessDetailsDao;
    }
    /**
     * 注入 制度来源
     */
    ClauseDao clauseDao;
    @Autowired(required = false)
    public void setClauseDao(ClauseDao clauseDao) {
        this.clauseDao = clauseDao;
    }
    /**
     * 注入
     */
    AuditDetailDao auditDetailDao;
    @Autowired(required = false)
    public void setAuditDetailDao(AuditDetailDao auditDetailDao) {
        this.auditDetailDao = auditDetailDao;
    }
    DetailDao detailDao;
    @Autowired(required = false)
    public void setDetailDao(DetailDao detailDao) {
        this.detailDao = detailDao;
    }

    /**
     * 注入用户
     */
    UserDao userDao;
    @Autowired(required = false)
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    /**
     * 注入部门
     */
    DeptDao deptDao;
    @Autowired(required = false)
    public void setDeptDao(DeptDao deptDao) {
        this.deptDao = deptDao;
    }

    /**
     * 新增DailyAssessDetails方法
     * @param dailyAssessDetails
     * @return
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public int insert(DailyAssessDetails dailyAssessDetails) {
        //将前端传过来的 考核项的字符串数组转换成逗号分割的字符串 再设置给当前 日考核对象
        String[] proList = dailyAssessDetails.getProjectIdList();
        String idStr = StringUtils.join(proList, ",");
        dailyAssessDetails.setProjectId(idStr);
        //获取创建人的姓名 并传给实体类
        User loginUser = SystemUtils.getLoginUser();
        Integer userId = loginUser.getId();
        dailyAssessDetails.setCreator(userId);
        dailyAssessDetails.toString();
        //通过部门id 去 拿到 部门名字存进去
        Integer deptId = dailyAssessDetails.getDeptId();
        String deptName = deptDao.findOneById(deptId).getName();
        dailyAssessDetails.setDeptName(deptName);
        //生成随机数
        String uuid = String2Int.get16UUID();
        dailyAssessDetails.setOrderNumber(uuid);
        //新增之后返回日考核详情id
        dailyAssessDetailsDao.insert(dailyAssessDetails);
        //将考核项的id全部取出来放到 detail里面去
        String projectId = dailyAssessDetails.getProjectId();
        int[] pidArr = String2Int.getIntArr(projectId);
        List project = dailyAssessDetails.getDetail();
        for (int i = 0; i < pidArr.length; i++) {
            project.add(pidArr[i]);
        }
        //设置详细考核项id
        dailyAssessDetails.setDetail(project);
        //新增中间表
//        dailyAssessDetailsDao.insertProjectDetail(dailyAssessDetails);
        //审核明细表新增数据
        AuditDetail auditDetail = new AuditDetail();
        //获取id
        Integer id = dailyAssessDetails.getId();
        //将id设置给审核明细表
        String userName = loginUser.getName();
        auditDetail.setCreateId(userName);
        auditDetail.setUpdated(userId);
        auditDetail.setDadId(id);
        Integer sourceSystem = dailyAssessDetails.getSourceSystem();
        auditDetail.setClauseId(sourceSystem);
        auditDetail.setStatus("0");
        //设置加分项目 查询到所有考核对象
        int[] intArr = String2Int.getIntArr(idStr);
        List<Detail> projectList = detailDao.findByIdStr(intArr);
        int totalScore = 0;
        BigDecimal totalMoney = new BigDecimal(0);
        for (Detail detail : projectList) {
            totalScore = totalScore + detail.getScore();
            totalMoney = totalMoney.add(detail.getMoney());
        }
        auditDetail.setMoney(totalMoney);
        auditDetail.setScore(totalScore);
        int code = auditDetailDao.insert(auditDetail);
        return code;
    }

    @Override
    public List<DailyAssessDetails> findDailyAssessPageAndAll() {
        return dailyAssessDetailsDao.findDailyAssessPageAndAll();
    }

    @Override
    public List<DailyAssessSummarize> findSummarize(DailyAssessSummarize dailyAssessSummarize) {

        return dailyAssessDetailsDao.findSummarize(dailyAssessSummarize);
    }

    @Override
    public int update(DailyAssessDetails dailyAssessDetails) {
        String clauseType = dailyAssessDetails.getClauseType();
        Clause clause =  clauseDao.findByName(clauseType);
        Integer id = clause.getId();
        dailyAssessDetails.setSourceSystem(id);
        return dailyAssessDetailsDao.updateByPrimaryKeySelective(dailyAssessDetails);
    }

    /**
     * 删除日考核详情 设置事务回滚
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public int deleteAssessDetail(DailyAssessDetails dailyAssessDetails) {
        dailyAssessDetailsDao.deleteAssessDetail(dailyAssessDetails);
        int id = dailyAssessDetails.getId();
        //通过id 去删除 审核详情表中的数据
        int code = auditDetailDao.deleteByDadId(id);
        return code;
    }

    @Override
    public List<DailyAssessDetails> findAssessDetailFuzzy(DailyAssessDetails dailyAssessDetails) {
        String assessPersonName = dailyAssessDetails.getAssessPersonName();
        //拿到当前被审核人的id
        User oneByName = null;
        if (assessPersonName!=null && assessPersonName !=""){
            oneByName = userDao.findOneByName(assessPersonName);
        }
        //设置 搜索的审核状态
        String isCheck = dailyAssessDetails.getIsCheck();
        if (isCheck!=null && isCheck!=""){
            dailyAssessDetails.setCheck(isCheck);
        }

        //设置 搜索的用户id
        if (oneByName != null){
            dailyAssessDetails.setUid(oneByName.getId());
        }else if (assessPersonName!=null && assessPersonName!=""){
            dailyAssessDetails.setUid(10000);
        }
        //设置 搜索的制度来源id
        if (dailyAssessDetails.getClauseType()!=null && dailyAssessDetails.getClauseType()!=""){
            dailyAssessDetails.setSourceSystem(clauseDao.findByName(dailyAssessDetails.getClauseType()).getId());
        }

        List<DailyAssessDetails> list =  dailyAssessDetailsDao.findAssessDetailFuzzy(dailyAssessDetails);
        return list;
    }

    @Override
    public DailyAssessDetails findById(String dadId) {
        int id = Integer.parseInt(dadId);
        return dailyAssessDetailsDao.findById(id);
    }

    @Override
    public Integer checkAssess(List<DailyAssessDetails> objList) {
        Integer code =  dailyAssessDetailsDao.updateByAssessList(objList);
        return code;
    }

    @Override
    public List<DailyAssessDetails> findAlNotCheck() {
        return dailyAssessDetailsDao.findAlNotCheck();
    }


}
